package com.shark.uh.controller;

import com.shark.uh.common.ApiResponse;
import com.shark.uh.model.dto.permission.PermissionCreateDTO;
import com.shark.uh.model.dto.permission.PermissionResponseDTO;
import com.shark.uh.model.dto.roles.RolesResponseDTO;
import com.shark.uh.model.query.PermissionQuery;
import com.shark.uh.service.PermissionsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("api/permissions")
public class PermissionController {
    @Resource
    private PermissionsService permissionsService;

    @PostMapping("/sava")
    public ApiResponse sava(@RequestBody PermissionCreateDTO permissionCreateDTO) {
        permissionsService.savePermissions(permissionCreateDTO);
        return ApiResponse.success();
    }

    @PostMapping("/delete")
    public ApiResponse delete(@RequestBody List<Long> permissionIds) {
        permissionsService.deletePermissions(permissionIds);
        return ApiResponse.success();
    }

    @PostMapping("/list")
    public ApiResponse<PermissionResponseDTO> list(@RequestBody PermissionQuery permissionQuery) {
        PermissionResponseDTO permissions = permissionsService.findPermissions(permissionQuery);
        return ApiResponse.success(permissions);
    }

    @PostMapping("/{permissionsId}/assign-roles")
    public ApiResponse assignRole(@PathVariable("permissionsId") Long permissionsId, @RequestBody List<Long> roleIds) {
        permissionsService.assignRoles(permissionsId, roleIds);
        return ApiResponse.success();
    }

    @PostMapping("/{permissionsId}/remove-roles")
    public ApiResponse removeRole(@PathVariable("permissionsId") Long permissionsId, @RequestBody List<Long> roleIds) {
        permissionsService.removeRoles(permissionsId, roleIds);
        return ApiResponse.success();
    }

    @GetMapping("/{permissionsId}/get/assign-roles")
    public ApiResponse<RolesResponseDTO> getAssignRoles(@PathVariable("permissionsId") Long permissionsId) {
        RolesResponseDTO assignRoles = permissionsService.getAssignRoles(permissionsId);
        return ApiResponse.success(assignRoles);
    }

}
